﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Xml.Linq;
namespace WPFExcelReport
{
    public partial class FLT2INTEX : BaseFunction
    {
        VABKOPF ABK;
        VABFOLGE ABF;
        VABAUFM MU;
        VABPOS ABP;
        VABPR ABPR;
        VGSCOLOR GSC;
        VGASTGA GS08;
        VGZKON GZ;
        VVAFARBEN VF;
        VGAAUF GA;

        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        public DataSet SetData = new DataSet();
        public DataSet SetDispo = new DataSet();
        public DateTime tomorrow = DateTime.Now.AddDays(1);
        public DateTime yesterday = DateTime.Now.AddDays(-1);
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "ITXVVVK ", ITXDATE = "20120723", ITXVKKRS = "C0", ITXVKLFD = "100227";

        public string today = "";
        public string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public string Left(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str + "                                                                                ";
                str = str.Substring(0, num);
            }
            catch (Exception ex)
            {

            }
            return str;
        }
        OracleCommand or1;
        public OracleConnection OracleDB;
        INTEXDBC INTEXDBC;
        public string ApplicationPath
        {
            get
            {
                return app.ApplicationPath;
            }
        }
        public FLT2INTEX(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {

            OracleDB = _OracleDB;
            proxy = _proxy;
        }
        /// <summary>
        /// 通过客户0实现不同的组合剪板条件设定。
        /// </summary>
        /// <param name="xdoc"></param>
        public bool Execute(XDocument xdoc = null)
        {
            bool flagValidate = false;
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            if (ABP == null) ABP = new VABPOS();
            if (ABPR == null) ABPR = new VABPR();
            if (GSC == null) GSC = new VGSCOLOR();
            if (GS08 == null) GS08 = new VGASTGA();
            if (GZ == null) GZ = new VGZKON();
            if (VF == null) VF = new VVAFARBEN();
            if (ABK == null) ABK = new VABKOPF();
            if (ABF == null) ABF = new VABFOLGE();
            if (MU == null) MU = new VABAUFM();
            if (GA == null) GA = new VGAAUF();
            DateTime now = DateTime.Now;
            today = now.Year.ToString() + Right("00" + now.Month.ToString(), 2) + Right("00" + now.Day.ToString(), 2);
            ITXDATE = today;
            if (SetData.Tables.Contains("GSCOLOR"))
            {
                foreach (DataRow row in SetData.Tables["GSCOLOR"].Rows)//已经填充了部分
                {
                    DataRow dwData = GSC.TBMain.NewRow();
                    GSC.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    GSC.DeleteGSCOLOR(dwData, 1);
                    GSC.InsertGSCOLOR(dwData, app.company);//这里就插入新数据库
                    EventDispatcher.Instance.RaiseStatus("插入GS07:" + row["GSCART"].ToString() + "_" + row["GSCFARBE"].ToString(), StatusEventType.RunningProgress);
                }
            }
            if (SetData.Tables.Contains("GASTGA"))
            {
                foreach (DataRow row in SetData.Tables["GASTGA"].Rows)
                { 
                    DataRow dwData = GS08.TBMain.NewRow();
                    GS08.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行 
                    GS08.DeleteGASTGA(dwData, 1);
                    GS08.InsertGASTGA(dwData, app.company);
                    EventDispatcher.Instance.RaiseStatus("插入GS08:" + row["GAGART"].ToString(), StatusEventType.RunningProgress);
                }
            }
            if (SetData.Tables.Contains("GZKON"))
            {
                foreach (DataRow row in SetData.Tables["GZKON"].Rows)
                {
                    DataRow dwData = GZ.TBMain.NewRow();
                    GZ.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    GZ.DeleteGZKON(dwData, 1);
                    GZ.InsertGZKON(dwData, app.company);
                    EventDispatcher.Instance.RaiseStatus("插入GZKON:" + row["GZKART"].ToString(), StatusEventType.RunningProgress);
                }
            }

            if (SetData.Tables.Contains("VAFARBEN"))
            {
                foreach (DataRow row in SetData.Tables["VAFARBEN"].Rows)
                {
                   
                    DataRow dwData = VF.TBMain.NewRow();
                    VF.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    VF.DeleteVAFARBEN(dwData, 1);
                    VF.InsertVAFARBEN(dwData, app.company);
                    EventDispatcher.Instance.RaiseStatus("插入VA10:" + row["VAFART"].ToString() + "_" + row["VAFFARBE"].ToString(), StatusEventType.RunningProgress);
                }
            }
            if (SetData.Tables.Contains("ABKOPF"))
            {
                foreach (DataRow row in SetData.Tables["ABKOPF"].Rows)
                {
                    #region============ABKOPF=====================
                    EventDispatcher.Instance.RaiseStatus("开始创建染纱ABKOPF:" + row["ABKAUFKRS"].ToString() + "_" + row["ABKAUFLFD"].ToString(), StatusEventType.RunningProgress);
                    DataRow dwData = ABK.TBMain.NewRow();
                    ABK.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    ABK.DeleteABKOPF(dwData, 1);
                    ABK.InsertABKOPF(dwData, app.company);
                    #endregion
                }
            }
            string client = "", krs = "", lfd = "", pos = "";
            if (SetData.Tables.Contains("ABPOS"))
            {
                foreach (DataRow row in SetData.Tables["ABPOS"].Rows)
                {
                    #region============ABPOS======================
                    EventDispatcher.Instance.RaiseStatus("开始创建染纱ABPOS:" + row["ABPAUFKRS"].ToString() + "_" + row["ABPAUFLFD"].ToString() + "_" + row["ABPAUFPOS"].ToString(), StatusEventType.RunningProgress);
                    client = row["ABPMANDANT"].ToString();
                    krs = row["ABPAUFKRS"].ToString();
                    lfd = row["ABPAUFLFD"].ToString();
                    pos = row["ABPAUFPOS"].ToString();
                    //INTEXDBC.SaveABPOS(row);
                    DataRow dwData = ABP.TBMain.NewRow();
                    ABP.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    ABP.DeleteABPOS(dwData, 1);
                    ABP.InsertABPOS(dwData, app.company);
                    ABF.DeleteABFOLGE(client, krs, lfd, pos);
                    #endregion
                }
            }
            if (SetData.Tables.Contains("ABPR"))
            {
                foreach (DataRow row in SetData.Tables["ABPR"].Rows)
                {
                    #region============ABPR=======================
                    EventDispatcher.Instance.RaiseStatus("开始创建染纱ABPR:" + row["ARAUFKRS"].ToString() + "_" + row["ARAUFLFD"].ToString() + "_" + row["ARAUFPOS"].ToString(), StatusEventType.RunningProgress);
                    //INTEXDBC.SaveABPR(row);
                    DataRow dwData = ABPR.TBMain.NewRow();
                    ABPR.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    ABPR.DeleteABPR(dwData, 1);
                    ABPR.InsertABPR(dwData, app.company);
                    #endregion
                }
            }
            if (SetData.Tables.Contains("ABAUFM"))
            {
                foreach (DataRow row in SetData.Tables["ABAUFM"].Rows)
                {
                    #region============ABAUFM=======================
                    EventDispatcher.Instance.RaiseStatus("开始创建染纱ABAUFM:", StatusEventType.RunningProgress);
                    DataRow dwData = MU.TBMain.NewRow();
                    MU.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    MU.DeleteABAUFM(dwData, 1);
                    MU.InsertABAUFM(dwData, app.company);
                    #endregion
                }
            }
            if (SetData.Tables.Contains("GAAUF"))
            {
                foreach (DataRow row in SetData.Tables["GAAUF"].Rows)
                {
                    #region============ABPR=======================
                    EventDispatcher.Instance.RaiseStatus("开始创建染纱GAAUF:" + row["GAAUFKRS"].ToString() + "_" + row["GAAUFLFD"].ToString() + "_" + row["GAAUFPOS"].ToString(), StatusEventType.RunningProgress);
                    DataRow dwData = GA.TBMain.NewRow();
                    GA.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    GA.DeleteGAAUF(dwData, 1);
                    GA.InsertGAAUF(dwData, 0);
                    #endregion
                }
            }
            if (SetData.Tables.Contains("ABFOLGE"))
            {
                foreach (DataRow row in SetData.Tables["ABFOLGE"].Rows)
                {
                    #region============ABFOLGE=======================
                    EventDispatcher.Instance.RaiseStatus("开始创建染纱ABFOLGE:" + row["ABFAUFKRS"].ToString() + "_" + row["ABFAUFLFD"].ToString() + "_" + row["ABFAUFPOS"].ToString(), StatusEventType.RunningProgress);
                    DataRow dwData = ABF.TBMain.NewRow();
                    ABF.SetUpNewRow(ref dwData, row);//创建符合目标数据库的行
                    ABF.InsertABFOLGE(dwData, app.company);
                    #endregion
                }
            }
            return true;
        }

        public void ReSet(DataSet setData, DataSet setDispo)
        {
            try
            {
                this.SetData = setData;
                this.SetDispo = setDispo;
                UserID = "INTEX   ";
                if (!this.SetData.Tables.Contains("INTEXMAP"))
                    return;
                if (this.SetData.Tables["INTEXMAP"].Rows.Count == 0)
                    return;
                DataRow dwIntex = this.SetData.Tables["INTEXMAP"].Rows[0];
                ITXMANDANT = dwIntex["ITXMANDANT"].ToString();
                ITXVKKRS = dwIntex["ITXVKKRS"].ToString();
                ITXVKLFD = dwIntex["ITXVKLFD"].ToString();
                ITXUSER = dwIntex["ITXUSER"].ToString();
                ITXDATE = dwIntex["ITXDATE"].ToString();
                ITXDBVERSION = dwIntex["ITXDBVERSION"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete()
        {
            try
            {

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}